package com.bigstar.lolen_e;

import java.util.HashMap;
import java.util.Random;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class CardTurn extends Activity implements OnClickListener {

	private boolean touch;
	public static boolean start = false;
	public Random r = new Random();
	public static HashMap<String, Bitmap> img = new HashMap<String, Bitmap>();
	public int gameTime = 0;
	public static final int CARD_COUNT = 30;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		ChampionData.load(this);
		ItemData.load(this);
		Rune.load(this);
		setContentView(R.layout.card_turn);
		AdView adview = (AdView) findViewById(R.id.adView);
		AdRequest re = new AdRequest();
		adview.loadAd(re);
		load();
		TextView tv = (TextView)this.findViewById(R.id.card_time);
		tv.setText("Funny Card Turn");
	}

	public void load() {
		img.clear();
		int w = this.getWindowManager().getDefaultDisplay().getWidth();
		int h = this.getWindowManager().getDefaultDisplay().getHeight();
		for(int i = 0; i < CARD_COUNT; i++){
			ChampionCard card = (ChampionCard) findViewById(R.id.card_00 + i);
			if(w == 480){
				card.getLayoutParams().width = 80;
				card.getLayoutParams().height = 80;	
			}else if(w == 720){
				card.getLayoutParams().width = 120;
				card.getLayoutParams().height = 120;	
			}else if(w == 800){
				card.getLayoutParams().width = 140;
				card.getLayoutParams().height = 140;
			}else if(w == 320){
				card.getLayoutParams().width = 60;
				card.getLayoutParams().height = 60;
			}else if(w == 240){
				card.getLayoutParams().width = 40;
				card.getLayoutParams().height = 40;
			}else{
				card.getLayoutParams().width = 80;
				card.getLayoutParams().height = 80;	
			}
			card.init();
		}
		gameTime = 0;
		start = false;
		touch = false;
		int[] temparray = new int[3];
		int[] map = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
				10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15 };
		for (int z = 0; z < 5; z++) {
			for (int x = 0; x < CARD_COUNT; x++) {
				temparray[0] = r.nextInt(CARD_COUNT);
				temparray[1] = map[temparray[0]];
				temparray[2] = map[x];
				map[x] = temparray[1];
				map[temparray[0]] = temparray[2];
			}
		}
		Bitmap back = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.champ_select);
		img.put("back", back);
		String[] nameMap = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
		for (int i = 0; i < CARD_COUNT/2; i++) {
			String name = "";
			boolean kk = true;
			name = ChampionData.champions.get(r.nextInt(ChampionData.champions
					.size())).name_en;
			for (int j = 0; j < i; j++) {
				if (nameMap[j].equals(name)) {
					kk = false;
					break;
				}
			}
			if (!kk) {
				i--;
				continue;
			}
			nameMap[i] = name;
			Bitmap b = Utils.loadBitmap(this, "champ_img/" + name + ".jpg");
			img.put(name, b);
		}
		for (int i = 0; i < CARD_COUNT; i++) {
			ChampionCard card = (ChampionCard) findViewById(R.id.card_00 + i);
			card.setOnClickListener(this);
			card.setName(nameMap[map[i]-1]);
			card.init();
			card.action();
		}
		Message msg = Message.obtain();
		msg.obj = "init";
		hHandler.sendMessageDelayed(msg, 5000);
	}
	
	@Override
	protected void onDestroy() {
		SharedPreferences pref = this.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int sec = (int) (pref.getLong("card_time", 0)/1000);
		int mil_sec = (int) (pref.getLong("card_time", 0)%1000);
		Toast.makeText(this, "best time : "+ sec+"."+mil_sec+"sec", Toast.LENGTH_SHORT).show();	
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		ChampionData.load(this);
		ItemData.load(this);
		Rune.load(this);
		super.onResume();
		for (int i = 0; i < CARD_COUNT; i++) {
			ChampionCard card = (ChampionCard) findViewById(R.id.card_00 + i);
		}
	}

	@Override
	public void onClick(View v) {
		if (!touch) {
			return;
		}
		for (int i = 0; i < CARD_COUNT; i++) {
			ChampionCard card = (ChampionCard) findViewById(R.id.card_00 + i);
			if (card.isAction()) {
				return;
			}
		}
		for (int i = 0; i < CARD_COUNT; i++) {
			if (v.getId() == R.id.card_00 + i) {
				ChampionCard card = (ChampionCard) findViewById(R.id.card_00 + i);
				if(!card.isSet()){
					card.action();
				}
			}
		}

	}
	
	private Handler hHandler  = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.obj.equals("init")){
				if(CardTurn.this.isFinishing()){
					return;
				}
				for (int i = 0; i < CARD_COUNT; i++) {
					ChampionCard card = (ChampionCard) findViewById(R.id.card_00 + i);
					card.action();
				}
				Message msg2 = Message.obtain();
				msg2.obj = "turn";
				hHandler.sendMessageDelayed(msg2, 2000);
			}else if(msg.obj.equals("turn")){
				if(CardTurn.this.isFinishing()){
					return;
				}
				start = true;
				touch = true;
				Toast.makeText(CardTurn.this, "start!!", Toast.LENGTH_SHORT).show();
				Message msg2 = Message.obtain();
				msg2.obj = "time";
				hHandler.sendMessageDelayed(msg2, 1000);
				TextView tv = (TextView)CardTurn.this.findViewById(R.id.card_time);
				gameTime = 0;
				tv.setText("game time : 60sec");
			}else if(msg.obj.equals("time")){
				if(CardTurn.this.isFinishing()){
					return;
				}
				gameTime += 50;
				int sec = (int) (gameTime / 1000);
				int mil_sec = (int) ((gameTime % 1000)/100);
				TextView tv = (TextView)CardTurn.this.findViewById(R.id.card_time);
				tv.setText("game time : "+sec+"."+mil_sec+"sec");
				Message msg2 = Message.obtain();
				msg2.obj = "time";
				hHandler.sendMessageDelayed(msg2, 50);
			}
		}
	};
}
